package com.usian.controller;

import com.uaisn.pojo.User;
import com.usian.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
public class ConUserController {

    @Autowired
    private RestTemplate restTemplate; //发起远程调用
    @Autowired
    private LoadBalancerClient loadBalancerClient; //去注册中心获取服务实例

    @Autowired
    UserFeign userFeign;

    @GetMapping("/findById")
    public User findById(Integer id){

        //1:找注册中心获取服务实例
        ServiceInstance instance = loadBalancerClient.choose("provider");
        //2:拼接访问地址  http://192.168.31.139:9090/user/findById/7
        String url ="http://"+instance.getHost()+":"+instance.getPort()+"/user/findById/"+id;
        //3:发起调用
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }

    @GetMapping("/findByIds")
    public User findByIds(Integer id,String name){
        User user = userFeign.findById(id,name);
        return user;
    }

    @GetMapping("/findByIdda")
    public User findByIdda(Integer id,String name){
        User user = userFeign.findByIdd(id,name);
        return user;
    }

    @PostMapping("/findByUser")
    public User findByUser(@RequestBody User user){
        System.out.println(user.toString());
        return userFeign.findByUser(user);
    }


}
